<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/10/10 20:51
 */

namespace module\ai\library\channel\llm;

use module\ai\library\channel\abstract\Llm;
use module\ai\library\channel\completion\ZhipuCompletion;
use module\ai\library\channel\interface\CompletionInterface;
use Psr\Http\Message\ResponseInterface;

class ZhipuGlm4 extends Llm
{

    protected string $baseUrl = 'https://open.bigmodel.cn/api/paas/v4/chat/completions';

    /**
     * @inheritDoc
     */
    protected function getCompletionStruct(array $data, bool $isStream = false): ?CompletionInterface
    {
        return new ZhipuCompletion($data, $isStream);
    }

    /**
     * @inheritDoc
     */
    public static function versions(): array
    {
        return [
            'glm-4-plus'   => 'glm-4-plus',
            'glm-4-0520'   => 'glm-4-0520',
            'glm-4-air'    => 'glm-4-air',
            'glm-4-airx'   => 'glm-4-airx',
            'glm-4-long'   => 'glm-4-long',
            'glm-4-flash'  => 'glm-4-flash(免费)',
            'glm-4-flashx' => 'glm-4-flashx',
        ];
    }

    /**
     * @inheritdoc
     */
    protected function getResponse(bool $isStream = false, array $clientOptions = []): ResponseInterface
    {
        return parent::getResponse($isStream, [
            'headers' => [
                'Authorization' => vsprintf("Bearer %s", [
                    $this->getKeyModel()?->config['api_key'],
                ]),
            ]
        ]);
    }

}